package com.paomi.client.activity.pay.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.paomi.client.R;
import com.paomi.client.bean.AddressInfo;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AddressListAdapter extends RecyclerView.Adapter<AddressListAdapter.DefaultViewHolder> {

    private Context context;
    private Typeface mTypeface;
    private List<AddressInfo> data = new ArrayList<>();
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        onItemClickListener = listener;
    }

    public AddressListAdapter(Context context, Typeface typeface) {
        this.context = context;
        this.mTypeface = typeface;
    }

    public void setData(List<AddressInfo> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @NonNull
    @Override
    public DefaultViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_defaul_address, parent, false);
        return new DefaultViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull DefaultViewHolder holder, int position) {
        AddressInfo addressInfo = data.get(position);
        holder.tvName.setText(addressInfo.getName());
        holder.tvPhone.setText(addressInfo.getMobile());
        holder.addressTv.setText(context.getResources().getString(R.string.address_tex) + addressInfo.getProvince() + addressInfo.getCity() + addressInfo.getDistrict() + addressInfo.getAddress());
        holder.tv_default.setText(context.getResources().getString(R.string.local_imag));
        holder.edit_tv.setText(context.getResources().getString(R.string.next_imag));
        holder.tv_default.setTypeface(mTypeface);
        holder.edit_tv.setTypeface(mTypeface);
        holder.line_add.setVisibility(View.GONE);
        holder.line_del.setVisibility(View.GONE);
        if (addressInfo.isDefault()) {
            holder.tv_mr.setVisibility(View.VISIBLE);
        } else {
            holder.tv_mr.setVisibility(View.GONE);
        }
        holder.edit_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener != null) {
                    onItemClickListener.setOnItemClick(holder.itemView, position);
                }
            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.change();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public class DefaultViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_default)
        TextView tv_default;//默认地址选中按钮
        @BindView(R.id.edit_tv)
        TextView edit_tv;//默认地址选中按钮
        @BindView(R.id.line_add)
        View line_add;
        @BindView(R.id.line_del)
        View line_del;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_phone)
        TextView tvPhone;
        @BindView(R.id.address_tv)
        TextView addressTv;
        @BindView(R.id.tv_mr)
        TextView tv_mr;

        public DefaultViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener {
        void setOnItemClick(View view, int pos);

        void change();
    }
}
